<?php

namespace app\common\model\user;

use app\common\model\User;
use think\Model;


class Level extends Model
{





    // 表名
    protected $name = 'user_level';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [];


    /**
     * 检查升级
     * @Author xy
     * @Date 2023-12-07 11:42:31
     * @LastEditors xy
     * @LastEditTime Do not edit
     * @Description 
     * @param {*} $user
     */
    static function checkUP($user)
    {

        if ($user->level_id < 4) {
            if ($user->team_consume >= config('site.uplevel4_team_consume')) {
                $has = User::where('pid', $user->id)->where('level_id', '>', 2)->count();
                if ($has >= config('site.uplevel4_team_vip')) {
                    $user->level_id = 4;
                    $user->save();
                }
            }
        }

        if ($user->level_id < 3) {
            if ($user->team_consume >= config('site.uplevel3_team_consume')) {
                $user->level_id = 3;
                $user->save();
            }
        }

        // if ($user->level_id == 4) {
        //     //检查董事
        //     $pids = explode(',', $user->team_tag);
        //     array_shift($pids);
        //     if (count($pids)) {
        //         $no = User::where('id', 'in', $pids)->where('level_id', '<', 5)->select();
        //         if ($no) {
        //             foreach ($no as  $u) {
        //                 //直推
        //                 $ids = User::where('pid', $u->id)->column('id');
        //                 if ($ids) {
        //                     $count = 0;
        //                     foreach ($ids as $id) {
        //                         $has =  User::where('find_in_set(:id,team_tag)', ['id' => $id])->where('level_id', 4)->where('is_formal_goudong', 1)->count();
        //                         if ($has) {
        //                             $count++;
        //                         }
        //                     }
        //                     if ($count >= config('site.ds_levelup')) {
        //                         //升级
        //                         $u->level_id = 5;
        //                         $u->save();
        //                     }
        //                 }
        //             }
        //         }
        //     }
        // }
    }
}
